<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@include file="../includes/tag.jsp" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    <script type="text/javascript" src="<%=basePath%>resources/lib/jquery/jquery.js"></script>
    <script type="text/javascript" src="<%=basePath%>resources/lib/webui/buss.ui.core.js"></script>
    <script type="text/javascript" src="<%=basePath%>resources/lib/webui/buss.ui.js"></script>
	<script type="text/javascript" src="<%=basePath%>resources/lib/jqgrid/js/i18n/grid.locale-cn.js"></script>
	<script type="text/javascript" src="<%=basePath%>resources/lib/jqgrid/js/jquery.jqGrid.min.js"></script>
	<script type="text/javascript" src="<%=basePath%>resources/lib/public.js"></script>
	
	<link rel="stylesheet" type="text/css" href="<%=basePath%>resources/lib/styles/themes/buss-ui.css">
	<link rel="stylesheet" type="text/css" href="<%=basePath%>resources/lib/jqgrid/css/jquery-ui.css">
	<link rel="stylesheet" type="text/css" href="<%=basePath%>resources/lib/jqgrid/css/ui.jqgrid.css">
    <link rel="stylesheet" type="text/css" href="<%=basePath%>resources/styles/public.css">
    <link rel="stylesheet" type="text/css" href="<%=basePath%>resources/styles/icon.css">

	<script type="text/javascript">
		$(document).ready(function(){		
			loadTree();
			setGridData();			
			resize();
			$(window).resize(resize);
			$("#add-btn").button({iconCls:'icon-add',handler:function(){
				var id = 0;
				var node = $("#dicDataTree").treeview("getSelectedNode");
				if(node){
					id = node.id;
				}
				dicDataEdit("add",id);
			}});		
			$("#edit-btn").button({iconCls:'icon-edit',handler:function(){
				var selRows = $("#tabledata").jqGrid('getGridParam',"selarrrow");
				if(selRows == 0){
					$.alert('提示','你没有选择要修改的数据!',300,140,'info');
					return;
				}
				if(selRows.length > 1){
					$.alert('提示','只能选择一条数据，请重新选择!',300,140,'info');
					return;
				}	
				dicDataEdit("edit",selRows[0]);
			}});
			$("#del-btn").button({iconCls:'icon-remove',handler:function(){
				var selRows = $("#tabledata").jqGrid('getGridParam',"selarrrow");
				if(selRows == 0){
					$.alert('提示','你没有选择要删除的数据!',300,140,'info');
					return;
				}
				$.confirm('提示','确认要删除选中数据吗?',300,140,function(){
					var str = "";
					for(var i = 0;i < selRows.length;i++){
						if(i < selRows.length - 1){
							str += "ids=" + selRows[i] + "&";
						}else{
							str += "ids=" + selRows[i];
						}
					}
					$.post("<%=basePath%>admin/dicdata/delete.do",str,function(rsData){
						if(rsData.message.code != '0'){
							$.alert('错误','删除数据字典信息失败!原因:' + rsData.message.mess,300,140,'error');
							return;
						}
						for(var i = 0;i < selRows.length;i++){
							 var node = $("#dicDataTree").treeview("getNodeByParam","id",selRows[i]); 
							 if(node){
								if(node.parentId == 0){
	        				 		$("#dicDataTree").treeview("removeNode",node);
								}
							 }
						}
						$("#tabledata").jqGrid({page:1}).trigger('reloadGrid');
					},"json");
				});				
			}});
			
			$("#search-btn").button({iconCls:'icon-search',handler:function(){
				var jsonParam = syscore.formToJson($("#_searchForm"));
				$("#tabledata").jqGrid("setGridParam",{postData:jsonParam}).trigger("reloadGrid");
			}});
			
			$("#search-clear").button({iconCls:'icon-remove',handler:function(){
				$("form[name='_searchForm']").resetForm();
				$("#search-btn").trigger("click");
			}});
		});
		
		function dicDataEdit(type,id){
			var pageUrl = "<%=basePath%>admin/dicdata/";
			var saveUrl = "<%=basePath%>admin/dicdata/";
			var title = "新增字典信息";
			if(type == "edit"){
				pageUrl += "editpage.do";
				saveUrl += "edit.do";
				title = "修改字典信息";
			}else{
				pageUrl += "addpage.do";
				saveUrl += "add.do";
			}
			var dlg = syscore.editDialog(title,pageUrl,'id=' + id,450,250,saveUrl,function(rsData){
				if(rsData.message.code != '0'){
					 $.alert('提示','保存数据失败!原因:' + rsData.message.mess,300,140,'error');
					 return false;
				 }else{
					 if(rsData.dicData.parentId == "0"){
						 if(type != "edit"){
						    var node = $("#dicDataTree").treeview("getNodeByParam","id","0");
						 	$("#dicDataTree").treeview("addNodes",node,rsData.dicData);
					 	 }else{
					 		var node = $("#dicDataTree").treeview("getNodeByParam","id",rsData.dicData.id);
					 		node.code = rsData.dicData.code;
					 		node.name = rsData.dicData.name;
					 		node.intro = rsData.dicData.intro;
		    				 $("#dicDataTree").treeview("updateNode",node);	
					 	 }
					 }					 
      			 	 $("#tabledata").jqGrid().trigger('reloadGrid');				        			 	
      			 	 dlg.dialog("destroy");
				 }
			},function(){
				
			});
		}
		
		function setGridData(){
			var url = "<%=basePath%>admin/dicdata/list/0.do";
    		var colNames = ["id","字典编码(Key)","字典名称(Value)","说明"];
    		var colModel = [
    		                {name:'id',index:'id',width:60,hidden:true},
    		                {name:'code',index:'code',width:100,align:'left'},
    		                {name:'name',index:'name',width:150,align:'left'},
    		                {name:'intro',index:'intro',width:300,align:'left'}
    		               ];
    		syscore.setGridData("#tabledata","#tablepage",url,colNames,colModel,true,0,'id');
		}	
		
		function loadTree(){
			var root = [{
				id:"0",
				name:'数据字典',			
				isParent:true,
				hasNode:true,
				open:true
			}];
			$("#dicDataTree").treeview({
				treeNodeKey:'id',
				treeNodeParentKey:'parentId',
				hasChildKey:'hasNode',
				nameCol:'name',
				nodesCol:'nodes',
				expandSpeed:"",
				asyncParam:['parentId'],
				async:true,
				root:{isRoot:true,nodes:root},
				asyncUrl:function(treeNode){
					return "<%=basePath%>admin/dicdata/list/0.do";
				},
				callback:{
					beforeAsyncSuccess:function(msg){
						if(msg.message.code == '-2'){
							$.alert('提示','你还没有登录或Session已经过期，请重新登录系统!',300,170,'info');
							return false;
						}
						if(msg.message.code == "-1"){
							$.alert("提示","获取信息失败，原因：" + msg.message.message,300,170,'error');
							return false;
						}
						return true;
					},
					click:function(event,treeId,treeNode){
						$("#tabledata").jqGrid("setGridParam",{"url":"<%=basePath%>admin/dicdata/list/" + treeNode.id + ".do"}).trigger('reloadGrid');
					}
				}
			});
		}

		function resize(){
			var h = $(window).height() - $(".nav_curloc").outerHeight() - $(".nav_search").outerHeight() - $(".nav_btn_bar").outerHeight() - 10;
			$(".context").height(h);
			$("#tabledata").jqGrid("setGridHeight",h - 50);
			$("#tabledata").jqGrid("setGridWidth",$(window).width() - $("#organTree").parent().width() - 12);
		}
		
	</script>
  </head>
  
  <body>
  	<div style="border-right:1px solid #68C4E9;position:absolute;width:180px;height:100%;left:0;overflow:auto;">
  		<ul id="dicDataTree" class="tree"></ul>
  	</div>
  	<div style="height:100%;margin:0 0 0 181px;position:relative;">
  		<div class="nav_curloc">
	    	<label>您的当前位置：<bstag:navbar menuId="${menuid}"/></label>
	    </div>
	     <div class="nav_search" style="height:28px;">
		     <form action="" id='_searchForm' name="_searchForm">
		    	<table height="100%" cellpadding="0" cellspacing="0">
		    		<tr>
		    			<td valign="middle"">字典名称：</td>
		    			<td valign="middle"">
		    				<input type="text" size="25" maxlength="100" class="text" name="name"/>
		    			</td>
		    			<td style="padding-left:5px;">
		    				<a id="search-btn" href="javascript:void(0);">查 询</a>
		    			</td>
		    			<td style="padding-left:5px;">
		    				<a id="search-clear" href="javascript:void(0);">清 空</a>
		    			</td>
		    		</tr>
		    	</table>
		    </form>
	    </div> 
	    <div class="nav_btn_bar">
	    	<table height="100%" cellpadding="0" cellspacing="0">
	    		<tr>
	    			<td valign="middle">
	    				<a id="add-btn" href="javascript:void(0);">新 增</a>
	    			</td>
	    			<td valign="middle"" style="padding-left:5px;">
	    				<a id="edit-btn" href="javascript:void(0);">修 改</a>
	    			</td>
	    			<td valign="middle" style="padding-left:5px;">
	    				<a id="del-btn" href="javascript:void(0);">删 除</a>
	    			</td>
	    		</tr>
	    	</table>
	    </div>
	    <div class="context">
	    	<table id="tabledata"></table>
	    	<div id="tablepage"></div>
	    </div>
  	</div> 
    
  </body>
</html>
